<?php

/**
 * This is the model class for table "cita".
 *
 * The followings are the available columns in table 'cita':
 * @property integer $idCita
 * @property integer $idCliente
 * @property string $fecha
 * @property string $hora
 * @property integer $idCitaEstado
 * @property integer $idDoctor
 * @property string $observaciones
 *
 * The followings are the available model relations:
 * @property Cliente $idCliente0
 * @property Citaestado $idCitaEstado0
 * @property Doctor $idDoctor0
 * @property Tratamientoscita[] $tratamientoscitas
 */
class Cita extends CActiveRecord
{

	public function getDoctorFullName()
	{
		$doctor = Doctor::model()->findByPk($this->idDoctor);
		return $doctor->nombre.' '.$doctor->apellido1;
	}
		
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Cita the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cita';
	}
	
	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('idCliente, fecha, hora, idCitaEstado, idDoctor, observaciones', 'required','message'=>'Por favor seleccione un tratamiento'),
			array('idCliente, idCitaEstado, idDoctor', 'numerical', 'integerOnly'=>true),
			array('observaciones', 'length', 'max'=>300),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idCita, idCliente, fecha, hora, idCitaEstado, idDoctor, observaciones', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'relcliente' => array(self::BELONGS_TO, 'Cliente', 'idCliente'),
			'relcitaEstado' => array(self::BELONGS_TO, 'Citaestado', 'idCitaEstado'),
			'reldoctor' => array(self::BELONGS_TO, 'Doctor', 'idDoctor'),
			'reltratamientoscitas' => array(self::HAS_MANY, 'Tratamientoscita', 'idCita'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idCita' => 'Número de Cita',
			'idCliente' => 'Cliente',
			'fecha' => 'Fecha',
			'hora' => 'Hora',
			'idCitaEstado' => 'Estado Actual',
			'idDoctor' => 'Doctor Asignado',
			'observaciones' => 'Tratamiento',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.
		$criteria=new CDbCriteria;
		
		$criteria->select = 'idCita, idCliente, fecha, hora, idCitaEstado,idDoctor,observaciones';
		//$criteria->condition = 'idCliente='.$id;
		$criteria->compare('idCita',$this->idCita);
		$criteria->compare('idCliente',$this->idCliente);
		$criteria->compare('fecha',$this->fecha,true);
		$criteria->compare('hora',$this->hora,true);
		$criteria->compare('idCitaEstado',$this->idCitaEstado);
		$criteria->compare('idDoctor',$this->idDoctor);
		$criteria->compare('observaciones',$this->observaciones,true);
		$criteria->order = 'fecha DESC';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
				'pageSize'=>5,
			),
		));
	}

	public static function getCitasIntervalo($startDate, $endDate){
		
		$citasIntervalo = array();
		$criteria= new CDbCriteria;
		$criteria->select = '*';
		$criteria->condition = 't.fecha >= \''.$startDate.'\' and t.fecha <= \''.$endDate.'\' and t.idCitaEstado = 1;';
		$citasIntervalo = Cita::model()->findAll($criteria);
		return $citasIntervalo;
	}
	
	

}